<?php
require_once("classes/functions.php");
require_once("classes/functions.php");
require_once("classes/page.class.php");

$page = new page();

$styles = array(
	'<link rel="stylesheet" type="text/css" href="css/global.css" />',
	'<link rel="stylesheet" type="text/css" href="css/layout.css" />',
	'<link rel="stylesheet" type="text/css" href="css/color.css" />'
);

$action = getFromInput("a");
switch ($action){
	case "save":
		if ($_FILES["uploadFile"]) {
			printHeader();
			echo '<div class="area" style="max-width:400px;">';
			
			//$fn = getFromInput("filename");
			$af = getFromInput("af");
			$ft = getFromInput("ft");
			$back_uri = getFromInput("back");
			$file = $_FILES["uploadFile"];
			$fn = $file["name"];
			
			$max_upload = get_max_upload_size();
			$filesize = intval($file["size"]);
			
			$ok=true;
			
			if (!$file["error"]) {
				$ext = getFileExtension($file["name"]);
				if ($ft == $ext) {
					if (move_uploaded_file($file["tmp_name"],"tmp/$fn")){
						echo '<h2>Fajl "'.$file["name"].'" je uspešno preuzet.</h2>';
					}else{
						$ok=false;
						echo "<h2>Greška pri kopiranju fajla.</h2>";
					}
				}else{
					$ok=false;
					echo "<h2>Greška!</h2>";
					echo "<p>Dozvoljeni tip fajla: $ft";
					echo "<br />Tip preuzetog fajla: $ext</p>";
				}
			}else{
				$ok=false;
				echo "<h2>Greška pri preuzimanju fajla.</h2>";
				echo "<p>Maksimalna dozvoljena veličina fajla: ".megakilobytes($max_upload)."</p>";
				
			}
			echo '<br clear="all" /><input type="button" onclick="location.href=\''.$back_uri.'&back='.urlencode($back_uri).'\'" value="&lt;&lt; nazad" />';
			if ($ok) {
				echo '<input style="float:right;" type="button" onclick="var p=(window.parent); p.'.$af.'(\'tmp/'.$fn.'\')" value="napred &gt;&gt;" />';
			}
			echo '</div>';
			printFooter();
		}
		break;
	default:
		$page->assign("styles",implode("\r\n",$styles));
		
		$afterfunc = getFromInput("af");
		$filename = getFromInput("fn");
		$filetype = getFromInput("ft");
		$back_uri = getFromInput("back");
		
		$page->assign("afterfunc",$afterfunc);
		$page->assign("filename",$filename);
		$page->assign("filetype",$filetype);
		$page->assign("back_uri",$back_uri);
		$page->assign("max_file_size",get_max_upload_size());
		
		echo $page->parseTemplate("upload_form.tpl");
		break;	
}


function printHeader(){
	global $styles;
	
	/*$dir = opendir("js");
	while ($f = readdir($dir)) {
		if (preg_match('/^[^\!].*?\.js$/',$f)) {
			echo '<script type="text/javascript" src="js/'.$f.'"></script>'."\r\n";
		}
	}*/
	
	echo '<html>';
	echo '<head>';
	echo '<meta http-equiv="content-type" content="text/html; charset=utf-8" />';
	echo implode("\r\n",$styles);
	echo '</head>';
	
	echo '<body class="iframe">';
	
}
function printFooter(){
	echo '</body>';
	echo '</html>';
}
function get_max_upload_size(){
	$max_upload = ini_get("upload_max_filesize");
	if (preg_match('/[^\d]/',$max_upload)) {
		$tmp = preg_split('/([^\d])/',$max_upload,-1,PREG_SPLIT_DELIM_CAPTURE);
		array_removeEmptyItems($tmp);
		$max_upload = "";
		foreach ($tmp as $item){
			$item = trim($item);
			if (preg_match('/^\d+$/',$item)) {
				$max_upload .= $item;
			}else{
				switch ($item){
					case "m":
					case "M":
						$max_upload .= "000000";
						break;
					case "k":
					case "K":
						$max_upload .= "000";
						break;
				}
			}
		}
	}
	$max_upload = intval($max_upload);
	return $max_upload;
}
function megakilobytes($size){
	if ($size>=1000000) {
		return bcdiv($size,1000000,0)." MB";
	}else if ($size>=1000) {
		return bcdiv($size,1000,0)." KB";
	}else{
		return "$size B";
	}
}
?>